﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebStudio.Business.Rule;
using WebStudio.Business.Entity;

public partial class Message : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            loadData();
        }
    }

    private string strWhere
    {
        get
        {
            if (string.IsNullOrEmpty(Request.QueryString["mid"]))
            {
                return "Status=1";
            }
            else
            {
                int mId = 0;
                try
                {
                    mId = Convert.ToInt32(Request.QueryString["mid"]);
                    return "MessageId=" + mId + "";
                }
                catch
                {
                    return "Status=1";
                }
            }
        }
    }

    private void loadData()
    {
        anpList.PageSize = 5;
        anpList.CurrentPageIndex = 1;
        anpList.RecordCount = MessageRule.GetMessageListCount(strWhere);
        rptMessageBind();
    }

    private void rptMessageBind()
    {
        rptMessage.DataSource = MessageRule.GetMessageList(anpList.CurrentPageIndex, anpList.PageSize, anpList.PageCount, anpList.RecordCount, strWhere);
        rptMessage.DataBind();
    }

    protected void anpList_PageChanged(object sender, EventArgs e)
    {
        rptMessageBind();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string userName = txtUserName.Text.Trim();
        string msgContent = txtMsg.Text.Trim();
        if (string.IsNullOrEmpty(userName))
        {
            ApplicationHelper.ShowClientMessage(this, "昵称不能为空");
            return;
        }
        if (string.IsNullOrEmpty(msgContent))
        {
            ApplicationHelper.ShowClientMessage(this, "留言内容不能为空");
            return;
        }
        MessageEntity info = new MessageEntity();
        info.MessageTitle = "";
        info.MessageContent = msgContent;
        info.SubmitDate = DateTime.Now;
        info.OwenerIP = Request.UserHostAddress.ToString();
        info.UserName = userName;
        info.Phone = txtPhone.Text.Trim();
        info.Email = txtEmail.Text.Trim();
        if (MessageRule.SaveMessageInfor(info))
        {
            ApplicationHelper.ShowClientMessage(this, "留言成功，谢谢您的支持！");
            rptMessageBind();
            txtEmail.Text = "";
            txtMsg.Text = "";
            txtPhone.Text = "";
            txtUserName.Text = "";
        }
        else
        {
            ApplicationHelper.ShowClientMessage(this, "留言失败");
            
        }
        return;
        
    }
}
